package com.hunzi.service.impl;

import com.hunzi.domain.OrderItem;
import com.hunzi.domain.Orders;
import com.hunzi.mapper.OrderItemMapper;
import com.hunzi.mapper.OrdersMapper;
import com.hunzi.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    //注入mapper
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private OrderItemMapper orderItemMapper;


    @Override
    public int deleteByPrimaryKey(String oid) {
        return 0;
    }

    //添加订单项
    @Override
    public int insert(Orders record) {
        return ordersMapper.insert ( record );
    }

    @Override
    public Orders selectByPrimaryKey(String oid) {
        return null;
    }

    @Override
    public List<Orders> selectAll() {
        return null;
    }

    @Override
    public int updateByPrimaryKey(Orders record) {
        return 0;
    }

    //生成订单
    @Override
    public void creatOrder(Orders orders) {
        //1.添加生成的订单 (重写insert方法)
        //ordersMapper.insert ( orders );
        this.insert ( orders );
        //2.添加订单项
        List<OrderItem> orderItems = orders.getOrderItems ();//得到订单项的集合
        //遍历添加的订单项
        for (OrderItem orderItem : orderItems) {
            //添加订单项
            orderItemMapper.insert ( orderItem );
        }
    }


    @Override
    public void updateOrdersAddress(Orders orders) {
        ordersMapper.updateOrdersAddress(orders);
    }

    @Override
    public void updateOrdersState(String oid, int state) {
        ordersMapper.updateOrdersState(oid,state);
    }
}
